
package com.example.m_kartupay.library;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class InquiryDatabaseHandler extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "android_api";

	// Login table name
	private static final String TABLE_INQUIRY = "inquiry";

	// Login Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_ACCOUNT_NUMBER = "account_number";
	private static final String KEY_BALANCE = "balance";
	private static final String KEY_POINTS = "points";


	public InquiryDatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_INQUIRY_TABLE = "CREATE TABLE " + TABLE_INQUIRY + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," 
				+ KEY_ACCOUNT_NUMBER + " TEXT,"
				+ KEY_BALANCE + " TEXT,"				
				+ KEY_POINTS + " TEXT" + ")";
		db.execSQL(CREATE_INQUIRY_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_INQUIRY);

		// Create tables again
		onCreate(db);
	}

	/**
	 * Storing user details in database
	 * */
	public void addInquiry(String account_number, String balance, String points) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(KEY_ACCOUNT_NUMBER, account_number); // Name
		values.put(KEY_BALANCE, balance); // Email
		values.put(KEY_POINTS, points); // Password
		

		// Inserting Row
		db.insert(TABLE_INQUIRY, null, values);
		db.close(); // Closing database connection
	}
	
	/**
	 * Getting user data from database
	 * */
	public HashMap<String, String> getUserDetails(){
		HashMap<String,String> user = new HashMap<String,String>();
		String selectQuery = "SELECT  * FROM " + TABLE_INQUIRY;
		 
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if(cursor.getCount() > 0){
        	user.put("account_number", cursor.getString(1));
        	user.put("balance", cursor.getString(2));
        	user.put("points", cursor.getString(3));
  
        }
        cursor.close();
        db.close();
		// return user
		return user;
	}

	/**
	 * Getting user login status
	 * return true if rows are there in table
	 * */
	public int getRowCount() {
		String countQuery = "SELECT  * FROM " + TABLE_INQUIRY;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		int rowCount = cursor.getCount();
		db.close();
		cursor.close();
		
		// return row count
		return rowCount;
	}
	
	/**
	 * Re crate database
	 * Delete all tables and create them again
	 * */
	public void resetTables(){
		SQLiteDatabase db = this.getWritableDatabase();
		// Delete All Rows
		db.delete(TABLE_INQUIRY, null, null);
		db.close();
	}

}
